<?php

require_once 'phpgen_settings.php';
require_once 'components/security/security_info.php';
require_once 'components/security/datasource_security_info.php';
require_once 'components/security/tablebased_auth.php';
require_once 'components/security/user_grants_manager.php';
require_once 'components/security/table_based_user_grants_manager.php';

require_once 'database_engine/mysql_engine.php';

$grants = array();

$appGrants = array();

$dataSourceRecordPermissions = array('maklumat_staf' => new DataSourceRecordPermission('id', false, false, false, true, true, true),
  'permohonan_kursus' => new DataSourceRecordPermission('id', false, false, false, true, true, true));

$tableCaptions = array('maklumat_staf' => 'maklumat_staf',
'permohonan_kursus' => 'permohonan_kursus',
'maklumat_kursus' => 'maklumat_kursus');

function CreateTableBasedGrantsManager()
{
    global $tableCaptions;
    $usersTable = array('TableName' => 'maklumat_staf', 'UserName' => 'nama', 'UserId' => 'id', 'Password' => 'noic');
    $userPermsTable = array('TableName' => 'phpgen_user_perms', 'UserId' => 'user_id', 'PageName' => 'page_name', 'Grant' => 'perm_name');

    $passwordHasher = HashUtils::CreateHasher(ENCRYPTION_NONE);
    $connectionOptions = GetGlobalConnectionOptions();
    $tableBasedGrantsManager = new TableBasedUserGrantsManager(new MySqlIConnectionFactory(), $connectionOptions,
        $usersTable, $userPermsTable, $tableCaptions, $passwordHasher, false);
    return $tableBasedGrantsManager;
}

function SetUpUserAuthorization()
{
    global $grants;
    global $appGrants;
    global $dataSourceRecordPermissions;
    $hardCodedGrantsManager = new HardCodedUserGrantsManager($grants, $appGrants);
$tableBasedGrantsManager = CreateTableBasedGrantsManager();
$grantsManager = new CompositeGrantsManager();
$grantsManager->AddGrantsManager($hardCodedGrantsManager);
if (!is_null($tableBasedGrantsManager))
    $grantsManager->AddGrantsManager($tableBasedGrantsManager);
$userAuthorizationStrategy = new TableBasedUserAuthorization(new MySqlIConnectionFactory(), GetGlobalConnectionOptions(), 'maklumat_staf', 'nama', 'id', $grantsManager);
    GetApplication()->SetUserAuthorizationStrategy($userAuthorizationStrategy);

GetApplication()->SetDataSourceRecordPermissionRetrieveStrategy(
    new HardCodedDataSourceRecordPermissionRetrieveStrategy($dataSourceRecordPermissions));
}

function GetIdentityCheckStrategy()
{
    return new TableBasedIdentityCheckStrategy(new MySqlIConnectionFactory(), GetGlobalConnectionOptions(), 'maklumat_staf', 'nama', 'noic', ENCRYPTION_NONE);
}

?>